/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.service.project;

import cn.ac.nercis.pes.common.utils.JsonUtils;
import cn.ac.nercis.pes.model.dto.common.Result;
import cn.ac.nercis.pes.repository.dal.project.SystemEvaluationRepository;
import cn.ac.nercis.pes.repository.model.project.SystemDO;
import cn.ac.nercis.pes.repository.model.project.SystemEvaluationDO;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * 系统测评基础信息服务
 *
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
@Service
public record SystemEvaluationService(SystemEvaluationRepository systemEvaluationRepository) {
    @Autowired
    public SystemEvaluationService {
    }

    public void initSystemEvaluation(@NonNull SystemDO systemDO, @NonNull SystemEvaluationDO evaluationDO){
        var result = save(systemDO.getId(),evaluationDO);
        if(!result.isSuccess()){
            log.error("保存系统[{}]测评基础信息失败：{}",systemDO.getId(), JsonUtils.toJson(evaluationDO));
        }
    }

    public SystemEvaluationDO getSystemEvaluation(@NonNull String systemId){
        return systemEvaluationRepository.findBySystemId(systemId)
                .orElse(null);
    }

    public Result<String> save(@NonNull String systemId, @NonNull SystemEvaluationDO evaluationDO){
        var entity = systemEvaluationRepository.findBySystemId(systemId);
        if(entity.isEmpty()){
            return create(systemId,evaluationDO);
        }else{
            return modify(entity.get(),evaluationDO);
        }
    }

    private Result<String> create(String systemId,SystemEvaluationDO entity){
        entity.setSystemId(systemId);
        entity.setCreateTime(LocalDateTime.now());
        return Optional.of(this.systemEvaluationRepository.save(entity))
                .map(SystemEvaluationDO::getId)
                .map(Result::success)
                .orElse(Result.failed());
    }

    private Result<String> modify(SystemEvaluationDO source,SystemEvaluationDO entity){
        entity.setId(source.getId());
        entity.setSystemId(source.getSystemId());
        entity.setCreateTime(source.getCreateTime());
        entity.setModifyTime(LocalDateTime.now());
        return Optional.of(this.systemEvaluationRepository.save(entity))
                .map(SystemEvaluationDO::getId)
                .map(Result::success)
                .orElse(Result.failed());
    }

}